﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace AGameOfThrones.drag
{
    class adorner : Adorner
    {
        UIElement _child;

         public adorner(UIElement element)
            : base(element)
        {
            VisualBrush _brush = new VisualBrush(element);
            _brush.Opacity = 0.8;
            
            Rectangle r = new Rectangle();
            r.Width = element.RenderSize.Width;
            r.Height = element.RenderSize.Height;
                          
            r.Fill = _brush;
            _child = r;  
        }

         public double XOrigin { get; set; }
         public double YOrigin { get; set; }        

        private double _leftOffset;
        public double LeftOffset
        {
            get { return _leftOffset; }
            set
            {
                _leftOffset = value - XOrigin;
                UpdatePosition();
            }
        }

        private double _topOffset;
        public double TopOffset
        {
            get { return _topOffset; }
            set
            {
                _topOffset = value - YOrigin;

                UpdatePosition();
            }
        }

        private void UpdatePosition()
        {
            AdornerLayer adorner = (AdornerLayer)this.Parent;
            if (adorner != null)
            {
                adorner.Update(this.AdornedElement);
            }
        }

        protected override Visual GetVisualChild(int index)
        {
            return this._child;
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }


        protected override Size MeasureOverride(Size finalSize)
        {
            this._child.Measure(finalSize);
            return this._child.RenderSize;
        }
        protected override Size ArrangeOverride(Size finalSize)
        {

            this._child.Arrange(new Rect(this._child.RenderSize));
            return finalSize;
        }

        public double scale = 1.0;
        public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
        {
            GeneralTransformGroup result = new GeneralTransformGroup();

            result.Children.Add(base.GetDesiredTransform(transform));
            result.Children.Add(new TranslateTransform(LeftOffset, TopOffset));
            return result;
        }
    }
}
